{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<style type='text/css'>\n",
       ".datatable table.frame { margin-bottom: 0; }\n",
       ".datatable table.frame thead { border-bottom: none; }\n",
       ".datatable table.frame tr.coltypes td {  color: #FFFFFF;  line-height: 6px;  padding: 0 0.5em;}\n",
       ".datatable .bool    { background: #DDDD99; }\n",
       ".datatable .object  { background: #565656; }\n",
       ".datatable .int     { background: #5D9E5D; }\n",
       ".datatable .float   { background: #4040CC; }\n",
       ".datatable .str     { background: #CC4040; }\n",
       ".datatable .row_index {  background: var(--jp-border-color3);  border-right: 1px solid var(--jp-border-color0);  color: var(--jp-ui-font-color3);  font-size: 9px;}\n",
       ".datatable .frame tr.coltypes .row_index {  background: var(--jp-border-color0);}\n",
       ".datatable th:nth-child(2) { padding-left: 12px; }\n",
       ".datatable .hellipsis {  color: var(--jp-cell-editor-border-color);}\n",
       ".datatable .vellipsis {  background: var(--jp-layout-color0);  color: var(--jp-cell-editor-border-color);}\n",
       ".datatable .na {  color: var(--jp-cell-editor-border-color);  font-size: 80%;}\n",
       ".datatable .footer { font-size: 9px; }\n",
       ".datatable .frame_dimensions {  background: var(--jp-border-color3);  border-top: 1px solid var(--jp-border-color0);  color: var(--jp-ui-font-color3);  display: inline-block;  opacity: 0.6;  padding: 1px 10px 1px 5px;}\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "import warnings\n",
    "warnings.simplefilter('ignore')\n",
    "\n",
    "import gc\n",
    "\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "pd.set_option('max_columns', 100)\n",
    "pd.set_option('max_rows', 100)\n",
    "from tqdm.notebook import tqdm\n",
    "\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.model_selection import GroupKFold\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "import lightgbm as lgb"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = pd.read_csv('raw_data/train.csv')\n",
    "train = train.sort_values(by=['QUEUE_ID', 'DOTTING_TIME']).reset_index(drop=True)\n",
    "\n",
    "test = pd.read_csv('raw_data/evaluation_public.csv')\n",
    "test = test.sort_values(by=['ID', 'DOTTING_TIME']).reset_index(drop=True)\n",
    "\n",
    "sub_sample = pd.read_csv('raw_data/submit_example.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>QUEUE_ID</th>\n",
       "      <th>CU</th>\n",
       "      <th>STATUS</th>\n",
       "      <th>QUEUE_TYPE</th>\n",
       "      <th>PLATFORM</th>\n",
       "      <th>CPU_USAGE</th>\n",
       "      <th>MEM_USAGE</th>\n",
       "      <th>LAUNCHING_JOB_NUMS</th>\n",
       "      <th>RUNNING_JOB_NUMS</th>\n",
       "      <th>SUCCEED_JOB_NUMS</th>\n",
       "      <th>CANCELLED_JOB_NUMS</th>\n",
       "      <th>FAILED_JOB_NUMS</th>\n",
       "      <th>DOTTING_TIME</th>\n",
       "      <th>RESOURCE_TYPE</th>\n",
       "      <th>DISK_USAGE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>3</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590683100000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>2</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590683400000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>7</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590683700000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>4</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590684000000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>5</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590684120000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>3</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590684420000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>2</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590684720000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>2</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590685020000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>5</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590685320000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>6</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1590685620000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   QUEUE_ID  CU     STATUS QUEUE_TYPE PLATFORM  CPU_USAGE  MEM_USAGE  \\\n",
       "0         2  16  available        sql   x86_64          3         54   \n",
       "1         2  16  available        sql   x86_64          2         54   \n",
       "2         2  16  available        sql   x86_64          7         54   \n",
       "3         2  16  available        sql   x86_64          4         54   \n",
       "4         2  16  available        sql   x86_64          5         54   \n",
       "5         2  16  available        sql   x86_64          3         55   \n",
       "6         2  16  available        sql   x86_64          2         54   \n",
       "7         2  16  available        sql   x86_64          2         54   \n",
       "8         2  16  available        sql   x86_64          5         54   \n",
       "9         2  16  available        sql   x86_64          6         54   \n",
       "\n",
       "   LAUNCHING_JOB_NUMS  RUNNING_JOB_NUMS  SUCCEED_JOB_NUMS  CANCELLED_JOB_NUMS  \\\n",
       "0                   0                 0                 0                   0   \n",
       "1                   0                 0                 0                   0   \n",
       "2                   0                 0                 0                   0   \n",
       "3                   0                 0                 0                   0   \n",
       "4                   0                 0                 0                   0   \n",
       "5                   0                 0                 0                   0   \n",
       "6                   0                 0                 0                   0   \n",
       "7                   0                 0                 0                   0   \n",
       "8                   0                 0                 0                   0   \n",
       "9                   0                 0                 0                   0   \n",
       "\n",
       "   FAILED_JOB_NUMS   DOTTING_TIME RESOURCE_TYPE  DISK_USAGE  \n",
       "0                0  1590683100000            vm        20.0  \n",
       "1                0  1590683400000            vm        20.0  \n",
       "2                0  1590683700000            vm        20.0  \n",
       "3                0  1590684000000            vm        20.0  \n",
       "4                0  1590684120000            vm        20.0  \n",
       "5                0  1590684420000            vm        20.0  \n",
       "6                0  1590684720000            vm        20.0  \n",
       "7                0  1590685020000            vm        20.0  \n",
       "8                0  1590685320000            vm        20.0  \n",
       "9                0  1590685620000            vm        20.0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "train.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>QUEUE_ID</th>\n",
       "      <th>CU</th>\n",
       "      <th>STATUS</th>\n",
       "      <th>QUEUE_TYPE</th>\n",
       "      <th>PLATFORM</th>\n",
       "      <th>CPU_USAGE</th>\n",
       "      <th>MEM_USAGE</th>\n",
       "      <th>LAUNCHING_JOB_NUMS</th>\n",
       "      <th>RUNNING_JOB_NUMS</th>\n",
       "      <th>SUCCEED_JOB_NUMS</th>\n",
       "      <th>CANCELLED_JOB_NUMS</th>\n",
       "      <th>FAILED_JOB_NUMS</th>\n",
       "      <th>DOTTING_TIME</th>\n",
       "      <th>RESOURCE_TYPE</th>\n",
       "      <th>DISK_USAGE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>60</td>\n",
       "      <td>69</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1662213420000</td>\n",
       "      <td>vm</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>58</td>\n",
       "      <td>69</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1662213720000</td>\n",
       "      <td>vm</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>80</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1662214020000</td>\n",
       "      <td>vm</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>100</td>\n",
       "      <td>65</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1662214320000</td>\n",
       "      <td>vm</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>16</td>\n",
       "      <td>available</td>\n",
       "      <td>sql</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>98</td>\n",
       "      <td>67</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1662214620000</td>\n",
       "      <td>vm</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>64</td>\n",
       "      <td>available</td>\n",
       "      <td>general</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>56</td>\n",
       "      <td>91</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1613655960000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>64</td>\n",
       "      <td>available</td>\n",
       "      <td>general</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>48</td>\n",
       "      <td>78</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1613656260000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>64</td>\n",
       "      <td>available</td>\n",
       "      <td>general</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>23</td>\n",
       "      <td>35</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1613656560000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>64</td>\n",
       "      <td>available</td>\n",
       "      <td>general</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>68</td>\n",
       "      <td>61</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1613656860000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>64</td>\n",
       "      <td>available</td>\n",
       "      <td>general</td>\n",
       "      <td>x86_64</td>\n",
       "      <td>38</td>\n",
       "      <td>74</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1613657160000</td>\n",
       "      <td>vm</td>\n",
       "      <td>20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  QUEUE_ID  CU     STATUS QUEUE_TYPE PLATFORM  CPU_USAGE  MEM_USAGE  \\\n",
       "0   1       297  16  available        sql   x86_64         60         69   \n",
       "1   1       297  16  available        sql   x86_64         58         69   \n",
       "2   1       297  16  available        sql   x86_64         80         67   \n",
       "3   1       297  16  available        sql   x86_64        100         65   \n",
       "4   1       297  16  available        sql   x86_64         98         67   \n",
       "5   2     85153  64  available    general   x86_64         56         91   \n",
       "6   2     85153  64  available    general   x86_64         48         78   \n",
       "7   2     85153  64  available    general   x86_64         23         35   \n",
       "8   2     85153  64  available    general   x86_64         68         61   \n",
       "9   2     85153  64  available    general   x86_64         38         74   \n",
       "\n",
       "   LAUNCHING_JOB_NUMS  RUNNING_JOB_NUMS  SUCCEED_JOB_NUMS  CANCELLED_JOB_NUMS  \\\n",
       "0                   0                 5                 5                   0   \n",
       "1                   0                 9                 4                   0   \n",
       "2                   0                 9                 1                   0   \n",
       "3                   0                 7                 2                   0   \n",
       "4                   0                10                 3                   0   \n",
       "5                   0                 0                 0                   0   \n",
       "6                   0                 1                 1                   0   \n",
       "7                   0                 0                 0                   0   \n",
       "8                   0                 0                 0                   0   \n",
       "9                   0                 0                 0                   0   \n",
       "\n",
       "   FAILED_JOB_NUMS   DOTTING_TIME RESOURCE_TYPE  DISK_USAGE  \n",
       "0                0  1662213420000            vm           9  \n",
       "1                0  1662213720000            vm           9  \n",
       "2                0  1662214020000            vm           9  \n",
       "3                1  1662214320000            vm           9  \n",
       "4                1  1662214620000            vm           9  \n",
       "5                0  1613655960000            vm          20  \n",
       "6                0  1613656260000            vm          20  \n",
       "7                0  1613656560000            vm          20  \n",
       "8                0  1613656860000            vm          20  \n",
       "9                0  1613657160000            vm          20  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>CPU_USAGE_1</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_1</th>\n",
       "      <th>CPU_USAGE_2</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_2</th>\n",
       "      <th>CPU_USAGE_3</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_3</th>\n",
       "      <th>CPU_USAGE_4</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_4</th>\n",
       "      <th>CPU_USAGE_5</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  CPU_USAGE_1  LAUNCHING_JOB_NUMS_1  CPU_USAGE_2  LAUNCHING_JOB_NUMS_2  \\\n",
       "0   1            0                     0            0                     0   \n",
       "1   2            0                     0            0                     0   \n",
       "2   3            0                     0            0                     0   \n",
       "3   4            0                     0            0                     0   \n",
       "4   5            0                     0            0                     0   \n",
       "\n",
       "   CPU_USAGE_3  LAUNCHING_JOB_NUMS_3  CPU_USAGE_4  LAUNCHING_JOB_NUMS_4  \\\n",
       "0            0                     0            0                     0   \n",
       "1            0                     0            0                     0   \n",
       "2            0                     0            0                     0   \n",
       "3            0                     0            0                     0   \n",
       "4            0                     0            0                     0   \n",
       "\n",
       "   CPU_USAGE_5  LAUNCHING_JOB_NUMS_5  \n",
       "0            0                     0  \n",
       "1            0                     0  \n",
       "2            0                     0  \n",
       "3            0                     0  \n",
       "4            0                     0  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_sample.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 这些 columns 在 test 只有单一值, 所以直接去掉\n",
    "\n",
    "del train['STATUS']\n",
    "del train['PLATFORM']\n",
    "del train['RESOURCE_TYPE']\n",
    "\n",
    "del test['STATUS']\n",
    "del test['PLATFORM']\n",
    "del test['RESOURCE_TYPE']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 时间排序好后也没什么用了\n",
    "\n",
    "del train['DOTTING_TIME']\n",
    "del test['DOTTING_TIME']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Label Encoding\n",
    "\n",
    "le = LabelEncoder()\n",
    "train['QUEUE_TYPE'] = le.fit_transform(train['QUEUE_TYPE'].astype(str))\n",
    "test['QUEUE_TYPE'] = le.transform(test['QUEUE_TYPE'].astype(str))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加个 id 后面方便处理\n",
    "train['myid'] = train.index\n",
    "test['myid'] = test.index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "6447e61b1c444d10bfa5fae215c393db",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=43.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "# 生成 target 列\n",
    "\n",
    "df_train = pd.DataFrame()\n",
    "\n",
    "for id_ in tqdm(train.QUEUE_ID.unique()):\n",
    "    tmp = train[train.QUEUE_ID == id_]\n",
    "    tmp['CPU_USAGE_next25mins'] = tmp['CPU_USAGE'].shift(-5)\n",
    "    tmp['LAUNCHING_JOB_NUMS_next25mins'] = tmp['LAUNCHING_JOB_NUMS'].shift(-5)\n",
    "    df_train = df_train.append(tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(501515, 14)\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>QUEUE_ID</th>\n",
       "      <th>CU</th>\n",
       "      <th>QUEUE_TYPE</th>\n",
       "      <th>CPU_USAGE</th>\n",
       "      <th>MEM_USAGE</th>\n",
       "      <th>LAUNCHING_JOB_NUMS</th>\n",
       "      <th>RUNNING_JOB_NUMS</th>\n",
       "      <th>SUCCEED_JOB_NUMS</th>\n",
       "      <th>CANCELLED_JOB_NUMS</th>\n",
       "      <th>FAILED_JOB_NUMS</th>\n",
       "      <th>DISK_USAGE</th>\n",
       "      <th>myid</th>\n",
       "      <th>CPU_USAGE_next25mins</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_next25mins</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "      <td>7</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>3</td>\n",
       "      <td>5.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>16</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>54</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>20.0</td>\n",
       "      <td>4</td>\n",
       "      <td>6.0</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   QUEUE_ID  CU  QUEUE_TYPE  CPU_USAGE  MEM_USAGE  LAUNCHING_JOB_NUMS  \\\n",
       "0         2  16           2          3         54                   0   \n",
       "1         2  16           2          2         54                   0   \n",
       "2         2  16           2          7         54                   0   \n",
       "3         2  16           2          4         54                   0   \n",
       "4         2  16           2          5         54                   0   \n",
       "\n",
       "   RUNNING_JOB_NUMS  SUCCEED_JOB_NUMS  CANCELLED_JOB_NUMS  FAILED_JOB_NUMS  \\\n",
       "0                 0                 0                   0                0   \n",
       "1                 0                 0                   0                0   \n",
       "2                 0                 0                   0                0   \n",
       "3                 0                 0                   0                0   \n",
       "4                 0                 0                   0                0   \n",
       "\n",
       "   DISK_USAGE  myid  CPU_USAGE_next25mins  LAUNCHING_JOB_NUMS_next25mins  \n",
       "0        20.0     0                   3.0                            0.0  \n",
       "1        20.0     1                   2.0                            0.0  \n",
       "2        20.0     2                   2.0                            0.0  \n",
       "3        20.0     3                   5.0                            0.0  \n",
       "4        20.0     4                   6.0                            0.0  "
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = df_train[df_train.CPU_USAGE_next25mins.notna()]\n",
    "# df_train['CPU_USAGE_next25mins'] /= 100\n",
    "\n",
    "print(df_train.shape)\n",
    "df_train.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "def run_lgb(df_train, df_test, target):\n",
    "    \n",
    "    feature_names = list(\n",
    "        filter(lambda x: x not in ['CPU_USAGE_next25mins', 'LAUNCHING_JOB_NUMS_next25mins', 'QUEUE_ID', 'myid'], df_train.columns))\n",
    "    \n",
    "    model = lgb.LGBMRegressor(num_leaves=32,\n",
    "                              max_depth=6,\n",
    "                              learning_rate=0.08,\n",
    "                              n_estimators=10000,\n",
    "                              subsample=0.8,\n",
    "                              feature_fraction=0.8,\n",
    "                              reg_alpha=0.5,\n",
    "                              reg_lambda=0.8,\n",
    "                              random_state=2020)\n",
    "    oof = []\n",
    "    prediction = df_test[['ID', 'QUEUE_ID', 'myid']]\n",
    "    prediction[target] = 0\n",
    "    df_importance_list = []\n",
    "    \n",
    "    kfold = GroupKFold(n_splits=5)\n",
    "    for fold_id, (trn_idx, val_idx) in enumerate(kfold.split(df_train, df_train[target], df_train['QUEUE_ID'])):\n",
    "        \n",
    "        X_train = df_train.iloc[trn_idx][feature_names]\n",
    "        Y_train = df_train.iloc[trn_idx][target]\n",
    "        X_val = df_train.iloc[val_idx][feature_names]\n",
    "        Y_val = df_train.iloc[val_idx][target]\n",
    "        \n",
    "        print('\\nFold_{} Training ================================\\n'.format(fold_id+1))\n",
    "        lgb_model = model.fit(X_train, \n",
    "                              Y_train,\n",
    "                              eval_names=['train', 'valid'],\n",
    "                              eval_set=[(X_train, Y_train), (X_val, Y_val)],\n",
    "                              verbose=10,\n",
    "                              eval_metric='mse',\n",
    "                              early_stopping_rounds=20)\n",
    "        \n",
    "        pred_val = lgb_model.predict(X_val, num_iteration=lgb_model.best_iteration_)\n",
    "        df_oof = df_train.iloc[val_idx][[target, 'myid', 'QUEUE_ID']].copy()\n",
    "        df_oof['pred'] = pred_val\n",
    "        oof.append(df_oof)\n",
    "        \n",
    "        pred_test = lgb_model.predict(df_test[feature_names], num_iteration=lgb_model.best_iteration_)\n",
    "        prediction[target] += pred_test / kfold.n_splits\n",
    "        \n",
    "        df_importance = pd.DataFrame({\n",
    "            'column': feature_names,\n",
    "            'importance': lgb_model.feature_importances_,\n",
    "        })\n",
    "        df_importance_list.append(df_importance)\n",
    "        \n",
    "        del lgb_model, pred_val, pred_test, X_train, Y_train, X_val, Y_val\n",
    "        gc.collect()\n",
    "        \n",
    "    return oof, prediction, df_importance_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Fold_1 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 70.6213\tvalid's l2: 43.2251\n",
      "[20]\ttrain's l2: 51.011\tvalid's l2: 43.5256\n",
      "[30]\ttrain's l2: 45.6821\tvalid's l2: 42.938\n",
      "Early stopping, best iteration is:\n",
      "[12]\ttrain's l2: 64.1332\tvalid's l2: 42.3578\n",
      "\n",
      "Fold_2 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 73.7756\tvalid's l2: 34.0144\n",
      "[20]\ttrain's l2: 53.6048\tvalid's l2: 37.1839\n",
      "Early stopping, best iteration is:\n",
      "[5]\ttrain's l2: 103.612\tvalid's l2: 32.0698\n",
      "\n",
      "Fold_3 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 44.8343\tvalid's l2: 263.153\n",
      "[20]\ttrain's l2: 35.0573\tvalid's l2: 246.156\n",
      "[30]\ttrain's l2: 32.2759\tvalid's l2: 256.397\n",
      "[40]\ttrain's l2: 30.8942\tvalid's l2: 259.654\n",
      "Early stopping, best iteration is:\n",
      "[24]\ttrain's l2: 33.6089\tvalid's l2: 245.896\n",
      "\n",
      "Fold_4 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 69.0314\tvalid's l2: 52.5163\n",
      "[20]\ttrain's l2: 49.9745\tvalid's l2: 48.0681\n",
      "[30]\ttrain's l2: 44.7301\tvalid's l2: 49.1175\n",
      "[40]\ttrain's l2: 43.0355\tvalid's l2: 50.4953\n",
      "Early stopping, best iteration is:\n",
      "[27]\ttrain's l2: 45.7881\tvalid's l2: 47.61\n",
      "\n",
      "Fold_5 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 59.9054\tvalid's l2: 142.311\n",
      "[20]\ttrain's l2: 45.2431\tvalid's l2: 121.342\n",
      "[30]\ttrain's l2: 41.1594\tvalid's l2: 113.142\n",
      "[40]\ttrain's l2: 39.7411\tvalid's l2: 110.506\n",
      "[50]\ttrain's l2: 39.0085\tvalid's l2: 107.754\n",
      "[60]\ttrain's l2: 38.5515\tvalid's l2: 109.024\n",
      "[70]\ttrain's l2: 38.226\tvalid's l2: 109.508\n",
      "Early stopping, best iteration is:\n",
      "[50]\ttrain's l2: 39.0085\tvalid's l2: 107.754\n"
     ]
    }
   ],
   "source": [
    "oof1, prediction1, df_importance_list1 = run_lgb(df_train, test, target='CPU_USAGE_next25mins')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>column</th>\n",
       "      <th>importance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>MEM_USAGE</td>\n",
       "      <td>156.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>CPU_USAGE</td>\n",
       "      <td>150.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>DISK_USAGE</td>\n",
       "      <td>125.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>CU</td>\n",
       "      <td>104.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>RUNNING_JOB_NUMS</td>\n",
       "      <td>76.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>SUCCEED_JOB_NUMS</td>\n",
       "      <td>73.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>QUEUE_TYPE</td>\n",
       "      <td>34.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>LAUNCHING_JOB_NUMS</td>\n",
       "      <td>6.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>FAILED_JOB_NUMS</td>\n",
       "      <td>3.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>CANCELLED_JOB_NUMS</td>\n",
       "      <td>0.4</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               column  importance\n",
       "0           MEM_USAGE       156.0\n",
       "1           CPU_USAGE       150.6\n",
       "2          DISK_USAGE       125.6\n",
       "3                  CU       104.8\n",
       "4    RUNNING_JOB_NUMS        76.8\n",
       "5    SUCCEED_JOB_NUMS        73.0\n",
       "6          QUEUE_TYPE        34.6\n",
       "7  LAUNCHING_JOB_NUMS         6.0\n",
       "8     FAILED_JOB_NUMS         3.8\n",
       "9  CANCELLED_JOB_NUMS         0.4"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_importance1 = pd.concat(df_importance_list1)\n",
    "df_importance1 = df_importance1.groupby(['column'])['importance'].agg(\n",
    "    'mean').sort_values(ascending=False).reset_index()\n",
    "df_importance1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MSE: 94.94211232399769\n"
     ]
    }
   ],
   "source": [
    "df_oof1 = pd.concat(oof1)\n",
    "score = mean_squared_error(df_oof1['CPU_USAGE_next25mins'], df_oof1['pred'])\n",
    "print('MSE:', score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    14980.000000\n",
       "mean        10.603659\n",
       "std          7.586795\n",
       "min          1.965355\n",
       "25%          4.789789\n",
       "50%          9.142856\n",
       "75%         12.838920\n",
       "max         56.264859\n",
       "Name: CPU_USAGE_next25mins, dtype: float64"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prediction1.CPU_USAGE_next25mins.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Fold_1 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 2.1918\tvalid's l2: 11.0655\n",
      "[20]\ttrain's l2: 1.99441\tvalid's l2: 10.6562\n",
      "[30]\ttrain's l2: 1.9289\tvalid's l2: 10.3935\n",
      "[40]\ttrain's l2: 1.89266\tvalid's l2: 10.2175\n",
      "[50]\ttrain's l2: 1.87113\tvalid's l2: 10.1394\n",
      "[60]\ttrain's l2: 1.85669\tvalid's l2: 10.0907\n",
      "[70]\ttrain's l2: 1.84423\tvalid's l2: 10.0616\n",
      "[80]\ttrain's l2: 1.83686\tvalid's l2: 10.0659\n",
      "[90]\ttrain's l2: 1.82871\tvalid's l2: 10.0306\n",
      "[100]\ttrain's l2: 1.82232\tvalid's l2: 10.0473\n",
      "[110]\ttrain's l2: 1.81907\tvalid's l2: 10.0447\n",
      "Early stopping, best iteration is:\n",
      "[93]\ttrain's l2: 1.82633\tvalid's l2: 10.0222\n",
      "\n",
      "Fold_2 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 3.94307\tvalid's l2: 2.24021\n",
      "[20]\ttrain's l2: 3.56212\tvalid's l2: 2.15194\n",
      "[30]\ttrain's l2: 3.41018\tvalid's l2: 2.13478\n",
      "[40]\ttrain's l2: 3.33111\tvalid's l2: 2.14066\n",
      "[50]\ttrain's l2: 3.29182\tvalid's l2: 2.13955\n",
      "Early stopping, best iteration is:\n",
      "[35]\ttrain's l2: 3.36385\tvalid's l2: 2.12988\n",
      "\n",
      "Fold_3 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 3.7986\tvalid's l2: 4.82274\n",
      "[20]\ttrain's l2: 3.50842\tvalid's l2: 4.2851\n",
      "[30]\ttrain's l2: 3.38725\tvalid's l2: 4.14412\n",
      "[40]\ttrain's l2: 3.29891\tvalid's l2: 4.02198\n",
      "[50]\ttrain's l2: 3.25648\tvalid's l2: 4.03557\n",
      "[60]\ttrain's l2: 3.22994\tvalid's l2: 3.9821\n",
      "[70]\ttrain's l2: 3.20285\tvalid's l2: 4.02764\n",
      "[80]\ttrain's l2: 3.18743\tvalid's l2: 4.04291\n",
      "Early stopping, best iteration is:\n",
      "[60]\ttrain's l2: 3.22994\tvalid's l2: 3.9821\n",
      "\n",
      "Fold_4 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 3.8359\tvalid's l2: 2.62357\n",
      "[20]\ttrain's l2: 3.40398\tvalid's l2: 2.70321\n",
      "Early stopping, best iteration is:\n",
      "[2]\ttrain's l2: 5.11178\tvalid's l2: 2.52477\n",
      "\n",
      "Fold_5 Training ================================\n",
      "\n",
      "Training until validation scores don't improve for 20 rounds\n",
      "[10]\ttrain's l2: 4.18689\tvalid's l2: 1.35312\n",
      "[20]\ttrain's l2: 3.75774\tvalid's l2: 1.35833\n",
      "[30]\ttrain's l2: 3.60053\tvalid's l2: 1.36284\n",
      "Early stopping, best iteration is:\n",
      "[12]\ttrain's l2: 4.07939\tvalid's l2: 1.35174\n"
     ]
    }
   ],
   "source": [
    "oof2, prediction2, df_importance_list2 = run_lgb(df_train, test, target='LAUNCHING_JOB_NUMS_next25mins')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>column</th>\n",
       "      <th>importance</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>LAUNCHING_JOB_NUMS</td>\n",
       "      <td>263.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>MEM_USAGE</td>\n",
       "      <td>187.8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>RUNNING_JOB_NUMS</td>\n",
       "      <td>166.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>CPU_USAGE</td>\n",
       "      <td>104.6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>SUCCEED_JOB_NUMS</td>\n",
       "      <td>98.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>DISK_USAGE</td>\n",
       "      <td>74.2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>CU</td>\n",
       "      <td>47.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>CANCELLED_JOB_NUMS</td>\n",
       "      <td>35.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>QUEUE_TYPE</td>\n",
       "      <td>31.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>FAILED_JOB_NUMS</td>\n",
       "      <td>24.6</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               column  importance\n",
       "0  LAUNCHING_JOB_NUMS       263.2\n",
       "1           MEM_USAGE       187.8\n",
       "2    RUNNING_JOB_NUMS       166.0\n",
       "3           CPU_USAGE       104.6\n",
       "4    SUCCEED_JOB_NUMS        98.2\n",
       "5          DISK_USAGE        74.2\n",
       "6                  CU        47.0\n",
       "7  CANCELLED_JOB_NUMS        35.0\n",
       "8          QUEUE_TYPE        31.0\n",
       "9     FAILED_JOB_NUMS        24.6"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_importance2 = pd.concat(df_importance_list2)\n",
    "df_importance2 = df_importance2.groupby(['column'])['importance'].agg(\n",
    "    'mean').sort_values(ascending=False).reset_index()\n",
    "df_importance2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MSE: 4.000169005621965\n"
     ]
    }
   ],
   "source": [
    "df_oof2 = pd.concat(oof2)\n",
    "score = mean_squared_error(df_oof2['LAUNCHING_JOB_NUMS_next25mins'], df_oof2['pred'])\n",
    "print('MSE:', score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    14980.000000\n",
       "mean         0.329041\n",
       "std          1.649481\n",
       "min         -0.097474\n",
       "25%          0.069156\n",
       "50%          0.092879\n",
       "75%          0.130210\n",
       "max         34.811653\n",
       "Name: LAUNCHING_JOB_NUMS_next25mins, dtype: float64"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prediction2.LAUNCHING_JOB_NUMS_next25mins.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>CPU_USAGE_1</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_1</th>\n",
       "      <th>CPU_USAGE_2</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_2</th>\n",
       "      <th>CPU_USAGE_3</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_3</th>\n",
       "      <th>CPU_USAGE_4</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_4</th>\n",
       "      <th>CPU_USAGE_5</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  CPU_USAGE_1  LAUNCHING_JOB_NUMS_1  CPU_USAGE_2  LAUNCHING_JOB_NUMS_2  \\\n",
       "0   1            0                     0            0                     0   \n",
       "1   2            0                     0            0                     0   \n",
       "2   3            0                     0            0                     0   \n",
       "3   4            0                     0            0                     0   \n",
       "4   5            0                     0            0                     0   \n",
       "\n",
       "   CPU_USAGE_3  LAUNCHING_JOB_NUMS_3  CPU_USAGE_4  LAUNCHING_JOB_NUMS_4  \\\n",
       "0            0                     0            0                     0   \n",
       "1            0                     0            0                     0   \n",
       "2            0                     0            0                     0   \n",
       "3            0                     0            0                     0   \n",
       "4            0                     0            0                     0   \n",
       "\n",
       "   CPU_USAGE_5  LAUNCHING_JOB_NUMS_5  \n",
       "0            0                     0  \n",
       "1            0                     0  \n",
       "2            0                     0  \n",
       "3            0                     0  \n",
       "4            0                     0  "
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_sample.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>QUEUE_ID</th>\n",
       "      <th>myid</th>\n",
       "      <th>CPU_USAGE_next25mins</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_next25mins</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>0</td>\n",
       "      <td>10.538106</td>\n",
       "      <td>0.064479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>1</td>\n",
       "      <td>10.260576</td>\n",
       "      <td>0.201328</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>2</td>\n",
       "      <td>15.082348</td>\n",
       "      <td>0.198362</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>3</td>\n",
       "      <td>55.178834</td>\n",
       "      <td>0.468154</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>4</td>\n",
       "      <td>33.661207</td>\n",
       "      <td>0.211956</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>5</td>\n",
       "      <td>20.234766</td>\n",
       "      <td>0.098941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>6</td>\n",
       "      <td>18.480754</td>\n",
       "      <td>0.131637</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>7</td>\n",
       "      <td>13.490999</td>\n",
       "      <td>0.113829</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>8</td>\n",
       "      <td>19.160936</td>\n",
       "      <td>0.116536</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>9</td>\n",
       "      <td>20.191499</td>\n",
       "      <td>0.103389</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  QUEUE_ID  myid  CPU_USAGE_next25mins  LAUNCHING_JOB_NUMS_next25mins\n",
       "0   1       297     0             10.538106                       0.064479\n",
       "1   1       297     1             10.260576                       0.201328\n",
       "2   1       297     2             15.082348                       0.198362\n",
       "3   1       297     3             55.178834                       0.468154\n",
       "4   1       297     4             33.661207                       0.211956\n",
       "5   2     85153     5             20.234766                       0.098941\n",
       "6   2     85153     6             18.480754                       0.131637\n",
       "7   2     85153     7             13.490999                       0.113829\n",
       "8   2     85153     8             19.160936                       0.116536\n",
       "9   2     85153     9             20.191499                       0.103389"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prediction = prediction1.copy()\n",
    "prediction = pd.merge(prediction, prediction2[['myid', 'LAUNCHING_JOB_NUMS_next25mins']], on='myid')\n",
    "\n",
    "prediction.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>QUEUE_ID</th>\n",
       "      <th>myid</th>\n",
       "      <th>CPU_USAGE_next25mins</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_next25mins</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>2</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>3</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>297</td>\n",
       "      <td>4</td>\n",
       "      <td>33</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>5</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>6</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>7</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>8</td>\n",
       "      <td>19</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>2</td>\n",
       "      <td>85153</td>\n",
       "      <td>9</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  QUEUE_ID  myid  CPU_USAGE_next25mins  LAUNCHING_JOB_NUMS_next25mins\n",
       "0   1       297     0                    10                              0\n",
       "1   1       297     1                    10                              0\n",
       "2   1       297     2                    15                              0\n",
       "3   1       297     3                    55                              0\n",
       "4   1       297     4                    33                              0\n",
       "5   2     85153     5                    20                              0\n",
       "6   2     85153     6                    18                              0\n",
       "7   2     85153     7                    13                              0\n",
       "8   2     85153     8                    19                              0\n",
       "9   2     85153     9                    20                              0"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 注意: 提交要求预测结果需为非负整数\n",
    "\n",
    "prediction['CPU_USAGE_next25mins'] = prediction['CPU_USAGE_next25mins'].apply(np.floor)\n",
    "prediction['CPU_USAGE_next25mins'] = prediction['CPU_USAGE_next25mins'].apply(lambda x: 0 if x<0 else x)\n",
    "prediction['CPU_USAGE_next25mins'] = prediction['CPU_USAGE_next25mins'].astype(int)\n",
    "prediction['LAUNCHING_JOB_NUMS_next25mins'] = prediction['LAUNCHING_JOB_NUMS_next25mins'].apply(np.floor)\n",
    "prediction['LAUNCHING_JOB_NUMS_next25mins'] = prediction['LAUNCHING_JOB_NUMS_next25mins'].apply(lambda x: 0 if x<0 else x)\n",
    "prediction['LAUNCHING_JOB_NUMS_next25mins'] = prediction['LAUNCHING_JOB_NUMS_next25mins'].astype(int)\n",
    "\n",
    "prediction.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>CPU_USAGE_1</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_1</th>\n",
       "      <th>CPU_USAGE_2</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_2</th>\n",
       "      <th>CPU_USAGE_3</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_3</th>\n",
       "      <th>CPU_USAGE_4</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_4</th>\n",
       "      <th>CPU_USAGE_5</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  CPU_USAGE_1  LAUNCHING_JOB_NUMS_1  CPU_USAGE_2  LAUNCHING_JOB_NUMS_2  \\\n",
       "0   1            0                     0            0                     0   \n",
       "1   2            0                     0            0                     0   \n",
       "2   3            0                     0            0                     0   \n",
       "3   4            0                     0            0                     0   \n",
       "4   5            0                     0            0                     0   \n",
       "\n",
       "   CPU_USAGE_3  LAUNCHING_JOB_NUMS_3  CPU_USAGE_4  LAUNCHING_JOB_NUMS_4  \\\n",
       "0            0                     0            0                     0   \n",
       "1            0                     0            0                     0   \n",
       "2            0                     0            0                     0   \n",
       "3            0                     0            0                     0   \n",
       "4            0                     0            0                     0   \n",
       "\n",
       "   CPU_USAGE_5  LAUNCHING_JOB_NUMS_5  \n",
       "0            0                     0  \n",
       "1            0                     0  \n",
       "2            0                     0  \n",
       "3            0                     0  \n",
       "4            0                     0  "
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub_sample.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "3ae6face142d417e8928d4df25213ebd",
       "version_major": 2,
       "version_minor": 0
      },
      "text/plain": [
       "HBox(children=(FloatProgress(value=0.0, max=2996.0), HTML(value='')))"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "preds = []\n",
    "\n",
    "for id_ in tqdm(prediction.ID.unique()):\n",
    "    items = [id_]\n",
    "    tmp = prediction[prediction.ID == id_].sort_values(by='myid').reset_index(drop=True)\n",
    "    for i, row in tmp.iterrows():\n",
    "        items.append(row['CPU_USAGE_next25mins'])\n",
    "        items.append(row['LAUNCHING_JOB_NUMS_next25mins'])\n",
    "    preds.append(items)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ID</th>\n",
       "      <th>CPU_USAGE_1</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_1</th>\n",
       "      <th>CPU_USAGE_2</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_2</th>\n",
       "      <th>CPU_USAGE_3</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_3</th>\n",
       "      <th>CPU_USAGE_4</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_4</th>\n",
       "      <th>CPU_USAGE_5</th>\n",
       "      <th>LAUNCHING_JOB_NUMS_5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "      <td>55</td>\n",
       "      <td>0</td>\n",
       "      <td>33</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>18</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>19</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>10</td>\n",
       "      <td>0</td>\n",
       "      <td>14</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>5</td>\n",
       "      <td>6</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>6</td>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>0</td>\n",
       "      <td>20</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>7</td>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "      <td>11</td>\n",
       "      <td>4</td>\n",
       "      <td>11</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>2</td>\n",
       "      <td>13</td>\n",
       "      <td>3</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "      <td>4</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>9</td>\n",
       "      <td>10</td>\n",
       "      <td>7</td>\n",
       "      <td>0</td>\n",
       "      <td>13</td>\n",
       "      <td>0</td>\n",
       "      <td>9</td>\n",
       "      <td>0</td>\n",
       "      <td>12</td>\n",
       "      <td>0</td>\n",
       "      <td>15</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   ID  CPU_USAGE_1  LAUNCHING_JOB_NUMS_1  CPU_USAGE_2  LAUNCHING_JOB_NUMS_2  \\\n",
       "0   1           10                     0           10                     0   \n",
       "1   2           20                     0           18                     0   \n",
       "2   3            8                     0           10                     0   \n",
       "3   4           12                     0           10                     0   \n",
       "4   5            5                     0            3                     0   \n",
       "5   6            9                     0            7                     0   \n",
       "6   7            8                     0           20                     0   \n",
       "7   8            2                    11            4                    11   \n",
       "8   9            4                     0            4                     0   \n",
       "9  10            7                     0           13                     0   \n",
       "\n",
       "   CPU_USAGE_3  LAUNCHING_JOB_NUMS_3  CPU_USAGE_4  LAUNCHING_JOB_NUMS_4  \\\n",
       "0           15                     0           55                     0   \n",
       "1           13                     0           19                     0   \n",
       "2           11                     0           13                     0   \n",
       "3           14                     0           11                     0   \n",
       "4            4                     0            3                     0   \n",
       "5            8                     0            8                     0   \n",
       "6           12                     0            9                     0   \n",
       "7            2                    13            2                    13   \n",
       "8            4                     0            4                     0   \n",
       "9            9                     0           12                     0   \n",
       "\n",
       "   CPU_USAGE_5  LAUNCHING_JOB_NUMS_5  \n",
       "0           33                     0  \n",
       "1           20                     0  \n",
       "2           12                     0  \n",
       "3           12                     0  \n",
       "4            3                     0  \n",
       "5            8                     0  \n",
       "6           11                     0  \n",
       "7            3                    13  \n",
       "8            4                     0  \n",
       "9           15                     0  "
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub = pd.DataFrame(preds)\n",
    "sub.columns = sub_sample.columns\n",
    "\n",
    "sub.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((2996, 11), (2996, 11))"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sub.shape, sub_sample.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub.to_csv('baseline_202010141435.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
